using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbNhaSXDAL
    {
        private SqlConnection cn = null;
        public tbNhaSXDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbNhaSXDAL(tbNhaSXDAL tuan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbNhaSX nhasx)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhaSX_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenNSX", nhasx.TenNSX);
            cm.Parameters.AddWithValue("@DiaChi", nhasx.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", nhasx.DienThoai);
            cm.ExecuteNonQuery();
        }
        public void Update(tbNhaSX nhasx)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhaSX_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaNSX", nhasx.MaNSX);
            cm.Parameters.AddWithValue("@TenNSX", nhasx.TenNSX);
            cm.Parameters.AddWithValue("@DiaChi", nhasx.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", nhasx.DienThoai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhaSX_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaNSX", iD);
            cm.ExecuteNonQuery();
        }
        public tbNhaSXCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbNhaSX_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbNhaSXCollection nsxCollection = new tbNhaSXCollection();

            while (dr.Read())
            {
                tbNhaSX nhasx = new tbNhaSX();
                nhasx.MaNSX = int.Parse(dr.GetValue(0).ToString());
                nhasx.TenNSX = dr.GetValue(1).ToString();
                nhasx.DiaChi = dr.GetValue(2).ToString();
                nhasx.DienThoai = dr.GetValue(3).ToString();
                nsxCollection.Add(nhasx);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return nsxCollection;
        }
    }
}
